﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using TimKen.BussLogic;
using System.Data.SqlClient;

namespace TimKen.Web.Admin.Product
{
    /// <summary>
    /// 显示促销产品列表，并进行挂历
    /// grid中提供取消促销按钮、
    /// </summary>
    public partial class ProductPromote_List : TimKen.App.AdminPage
    {
        private ProductManager productManager = new ProductManager();
        private PCategoryManager categoryManager = new PCategoryManager();
        private string toadd = "├";

        public override string ActionCode
        {
            get
            {
                return "90";
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                //绑定产品分类
                this.ddlCategory.Items.Clear();
                this.ddlCategory.Items.Add(new ListItem(GetLangString("ChooseCategory"), "-1"));
                this.ddlCategory.Items.Add(new ListItem(GetLangString("SetAsRootCategory"), "0"));

                //绑定品牌列表
                BindBrandList();
                BindProductCategory("0");
                BindIsPromoteList();
                BindData();
                BindResources();
            }
        }

        protected void BindData()
        {
            int categoryId = int.Parse(this.ddlCategory.SelectedValue);
            int brandId = int.Parse(this.ddlBrand.SelectedValue);
            string keyWords = this.txtKeywords.Text.Trim();
            string isPromote = this.ddlIsPromote.SelectedValue;

            DataSet ds = productManager.GetPromoteList(AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex, LanguageID, brandId, categoryId, keyWords, isPromote, false);
            AspNetPager1.RecordCount = Convert.ToInt32(ds.Tables[1].Rows[0]["total"]);

            this.grdProductPromote.DataSource = ds;
            this.grdProductPromote.DataBind();
        }

        public void BindResources()
        {
            btnSearch.Text = GetLangString("Search");
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindData();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindData();
        }

        /// <summary>
        /// 绑定产品分类分级选择列表
        /// </summary>
        private void BindProductCategory(string parentId)
        {
            DataSet ds = categoryManager.GetList(LanguageWhereStr + " AND ParentID=" + parentId);
            if (ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    this.ddlCategory.Items.Add(new ListItem(toadd + " " + dr["CategoryName"].ToString(), dr["CategoryID"].ToString()));
                    toadd += "─┴";
                    BindProductCategory(dr["CategoryID"].ToString());
                    toadd = toadd.Substring(0, toadd.Length - 2);
                }
            }
        }

        private void BindIsPromoteList()
        {
            this.ddlIsPromote.Items.Add(new ListItem(GetLangString("All"), "-1"));
            ListItem YLitem = new ListItem(GetLangString("InPromote"), "1");
            YLitem.Selected = true;
            this.ddlIsPromote.Items.Add(YLitem);
            this.ddlIsPromote.Items.Add(new ListItem(GetLangString("NoPromote"), "0"));
        }

        private void BindBrandList()
        {
            string sql = "SELECT  BrandID,Name FROM PBrand";
            SqlDataReader reader = DataAccess.DbHelperSQL.ExecuteReader(sql);
            this.ddlBrand.Items.Add(new ListItem(GetLangString("ChooseBrand"), "0"));

            while (reader.Read())
            {
                this.ddlBrand.Items.Add(new ListItem(reader["Name"].ToString(), reader["BrandID"].ToString()));
            }

            reader.Close();
            reader.Dispose();
        }

        protected void ddlCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindData();
        }

        protected void ddlBrand_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindData();
        }
        //GridView的行更新事件
        protected void grdProductPromote_RowEditing(object sender, GridViewEditEventArgs e)
        {
            this.grdProductPromote.EditIndex = e.NewEditIndex;
            BindData();
        }

        protected void grdProductPromote_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            string ProductID = this.grdProductPromote.DataKeys[e.RowIndex].Value.ToString();
            DataEntity.Product product = productManager.GetModel(int.Parse(ProductID));

            TextBox PromoteFrom = this.grdProductPromote.Rows[e.RowIndex].FindControl("txtPromoteFrom") as TextBox;
            TextBox PromoteTo = this.grdProductPromote.Rows[e.RowIndex].FindControl("txtPromoteTo") as TextBox;
            TextBox PromoteDiscount = this.grdProductPromote.Rows[e.RowIndex].FindControl("txtPromoteDiscount") as TextBox;
            CheckBox IsPromote = this.grdProductPromote.Rows[e.RowIndex].FindControl("cbIsPromote") as CheckBox;

            if (IsPromote.Checked)
            {
                if (string.IsNullOrEmpty(PromoteFrom.Text) || string.IsNullOrEmpty(PromoteTo.Text) || string.IsNullOrEmpty(PromoteDiscount.Text))
                {
                    Alert("输入数据不完整");
                    return;
                }


                try
                {
                    product.IsPromote = IsPromote.Checked;
                    product.PromoteFrom = Convert.ToDateTime(PromoteFrom.Text);
                    product.PromoteTo = Convert.ToDateTime(PromoteTo.Text);
                    product.PromoteDiscount = Decimal.Parse(PromoteDiscount.Text);

                    if (product.PromoteDiscount * 100 > 100)
                        throw new Exception();
                }
                catch
                {
                    Alert(GetLangString("InputInvalid"));
                    return;
                }
                productManager.SetPromote(product.ProductID, product.IsPromote, product.PromoteDiscount, product.PromoteFrom, product.PromoteTo);
            }
            else
            {
                productManager.SetPromote(product.ProductID, false, null, null, null);
            }
            this.grdProductPromote.EditIndex = -1;
            BindData();
        }

        protected void grdProductPromote_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            this.grdProductPromote.EditIndex = -1;
            BindData();
        }
    }
}
